package com.upc.toss.util;



import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.text.BreakIterator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HttpsURLConnection;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class DataBaseController {
    URL url;
    public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
    private String token;
    private String cookie;

    private String baseUrl="http://1.116.33.97:8080";

//  private String baseUrl="http://172.24.90.255:8080";


    public String getToken(){
        return this.token;
    }

    public int isLogin(String username,String password){
        URL url= null;
        String json="{\"username\":\""+username+"\",\"password\":\""+password+"\"}";
        try {
            url = new URL(baseUrl+"/login");//连接到后端的登录功能
            OkHttpClient okHttpClient=new OkHttpClient.Builder()
                    .connectTimeout(3, TimeUnit.SECONDS)
                    .build();
            RequestBody requestBody=RequestBody.create(JSON,json);
            Request request = new Request.Builder()
                    .post(requestBody)
                    .url(url)
                    .build();
            Call call =okHttpClient.newCall(request);
            Response response=call.execute();
            JSONObject resObject=new JSONObject((response.body().string()));
            int c=(Integer) resObject.get("code");
            if(c==100){
                JSONObject dataObj = (JSONObject) resObject.get("data");
                token=(String)dataObj.get("token");
            }
            return c;
        } catch (IOException e) {
            e.printStackTrace();
            return -500;
        }catch (JSONException e){
            e.printStackTrace();
            return -2;
        }
    }

    public int register(String username,String password,String cap){
        URL url= null;
        String json="{\"username\":\""+username+"\",\"password\":\""+password+"\"}";

        try {
            url = new URL(baseUrl+"/register?verCode="+cap);
            OkHttpClient okHttpClient=new OkHttpClient.Builder()
                    .connectTimeout(3, TimeUnit.SECONDS)
                    .build();
            RequestBody requestBody=RequestBody.create(JSON,json);
            Request request = new Request.Builder()
                    .post(requestBody)
                    .url(url)
                    .addHeader("Cookie",cookie)
                    .build();
            Call call =okHttpClient.newCall(request);
            Response response=call.execute();
            JSONObject resObject=new JSONObject((response.body().string()));
            int c=(Integer) resObject.get("code");
            if (c==100) return 1;
            else if(c==-1) return -1;
            else return 0;
        } catch (IOException e) {
            e.printStackTrace();
            return -2;
        }catch (JSONException e){
            e.printStackTrace();
            return -3;
        }
    }

    public HashMap<String,Object> getAdvice(String token,String variety){
        HashMap<String,Object> resMap=new HashMap<>();
        URL url= null;
        String json="{\"variety\":\""+variety+"\"}";
        try {
            url = new URL(baseUrl+"/advice");
            OkHttpClient okHttpClient=new OkHttpClient.Builder()
                    .connectTimeout(3, TimeUnit.SECONDS)
                    .build();

            RequestBody requestBody=RequestBody.create(JSON,json);
            Request request = new Request.Builder()
                    .post(requestBody)
                    .url(url)
                    .addHeader("token",token)
                    .build();
            Call call =okHttpClient.newCall(request);
            Response response=call.execute();

            JSONObject resObject=new JSONObject((response.body().string()));
            JSONObject dataObj= (JSONObject) resObject.get("data");
            resMap.put("appetite",dataObj.get("appetite"));
            resMap.put("temperature",dataObj.get("temperature"));

            return resMap;

        }  catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return resMap;
    }

    public String getFile(String token,String name){
        URL url= null;
        try {
            url = new URL(baseUrl + "/fileDownload?name="+name);
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(3, TimeUnit.SECONDS)
                    .build();

            Request request = new Request.Builder()
                    .get()
                    .url(url)
                    .addHeader("token",token)
                    .build();
            Call call = okHttpClient.newCall(request);
            Response response = call.execute();
            JSONObject resObject = new JSONObject((response.body().string()));
            if (100 ==(int) resObject.get("code")){
                String base64=(String) resObject.get("data");
                return base64;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "-100";
    }


    public boolean isExpired(String token){
        URL url= null;
        try {
            url = new URL( baseUrl+"/outdated");
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(3, TimeUnit.SECONDS)
                    .build();
            Request request = new Request.Builder()
                    .url(url)
                    .addHeader("token",token)
                    .build();
            Call call = okHttpClient.newCall(request);
            Response response = call.execute();
            JSONObject resObject=new JSONObject((response.body().string()));
            int code=(int)resObject.get("code");
            return code != 100;
    } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean updateFile(String token,String data,String name){
        URL url= null;
        String json="{\"head_photo\":\""+data+"\",\"name\":\""+name+"\"}";
        try {
            url = new URL(baseUrl + "/fileUpload");
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(5, TimeUnit.SECONDS)
                    .build();
            RequestBody requestBody=RequestBody.create(JSON,json);
            Request request = new Request.Builder()
                    .post(requestBody)
                    .url(url)
                    .addHeader("token",token)
                    .build();
            Call call = okHttpClient.newCall(request);
            Response response = call.execute();
            JSONObject resObject = new JSONObject((response.body().string()));
            if (100 ==(int) resObject.get("code")){
                return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    public int addDevice(){
        return 1;
    }

    public int showDevice(){
        return 1;
    }

    public int deleteDevice(){
        return 1;
    }

}

